package sunwin.yoms.web.controller.system;

import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.FileHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.domain.park.ParkCooperateDomain;
import sunwin.yoms.service.comm.YomsDefaultData;
import sunwin.yoms.service.system.ParkCooperateService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsBase64Image;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.resolverArg.YomsArrayParam;
import sunwin.yoms.web.comm.resolverArg.YomsDateParam;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by watson on 2015/8/20.
 */
@Controller
public class ParkCooperateController {
    @Autowired
    private ParkCooperateService parkCooperateService;
    private final String FindPvm = "system/cooperate.find";
    private final String CreatePvm = "system/cooperate.create";
    private final String EditPvm = "system/cooperate.edit";
    private final String DelPvm = "system/cooperate.delete";

    /**
     * 查找公园
     */
    @RequestMapping(value = R.FindPark, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindPark, viewName = FindPvm)
    public ModelAndView findPark(
            ParkCooperateDomain parks,
            @YomsDateParam JDateTime startTime,
            @YomsDateParam JDateTime endTime,
            ModelMap modelMap) throws YomsException {
        modelMap.addAttribute("parkCooperate", parks);
        modelMap.addAttribute("startTime", startTime == null ? null : startTime.toString("YYYY-MM-DD hh:mm"));
        modelMap.addAttribute("endTime", endTime == null ? null : endTime.toString("YYYY-MM-DD hh:mm"));
        Date date1 = startTime == null ? null : startTime.convertToDate();
        Date date2 = endTime == null ? null : endTime.convertToDate();
        List<ParkCooperateDomain> parkCooperateDomains = parkCooperateService.findpark(parks, date1, date2);
        ModelAndView mav = new ModelAndView();
        mav.addObject("parkCooperateList", parkCooperateDomains);
        return mav;
    }

    /**
     * 编辑访问公园页面
     */
    @RequestMapping(value = R.EditPark, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.EditPark, viewName = EditPvm, isAjax = true)
    public ModelAndView EditPark(int id) throws YomsException {
        ParkCooperateDomain parkCooperateDomain = new ParkCooperateDomain();
        parkCooperateDomain = parkCooperateService.getParkById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("parks", parkCooperateDomain);
        return mav;
    }

    /**
     * 编辑公园
     *
     * @param parkCooperateDomain
     * @return
     * @throws YomsException
     */
    @RequestMapping(value = R.EditPark, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.EditPark, viewName = EditPvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer EditPark(ParkCooperateDomain parkCooperateDomain,
                                   @YomsDateParam JDateTime date1,
                                   @YomsDateParam JDateTime date2) throws YomsException {
        recompileImg(parkCooperateDomain);
        if (date1 == null) {
            parkCooperateDomain.setParkStartTime(new JDateTime(System.currentTimeMillis()).convertToDate());
        } else {
            parkCooperateDomain.setParkStartTime(date1.convertToDate());
        }
        if (date2 == null) {
            Calendar cld = Calendar.getInstance();

            cld.setTime(parkCooperateDomain.getParkStartTime());

            cld.add(Calendar.YEAR, +1000);
            parkCooperateDomain.setParkEndTime(cld.getTime());
        } else {
            parkCooperateDomain.setParkEndTime(date2.convertToDate());

        }
        parkCooperateService.upPark(parkCooperateDomain);
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    /**
     * 创建公园页面
     */
    @RequestMapping(value = R.CreatePark, method = RequestMethod.GET)
    @YomsController(viewName = CreatePvm)
    public void CreatePark() {
    }

    /**
     * 创建公园页面
     */
    @RequestMapping(value = R.CreatePark, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.CreatePark, viewName = CreatePvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer CreatePark(ParkCooperateDomain parkCooperateDomain,
                                     @YomsDateParam JDateTime date1,
                                     @YomsDateParam JDateTime date2) throws YomsException {
        String img = parkCooperateDomain.getParkImg();
        String content = parkCooperateDomain.getParkContent();
        parkCooperateDomain.setParkImg("");
        parkCooperateDomain.setParkContent("");
        if (date1 == null) {
            parkCooperateDomain.setParkStartTime(new JDateTime(System.currentTimeMillis()).convertToDate());
        } else {
            parkCooperateDomain.setParkStartTime(date1.convertToDate());
        }
        if (date2 == null) {
            Calendar cld = Calendar.getInstance();

            cld.setTime(parkCooperateDomain.getParkStartTime());

            cld.add(Calendar.YEAR, +1000);
            parkCooperateDomain.setParkEndTime(cld.getTime());
        } else {
            parkCooperateDomain.setParkEndTime(date2.convertToDate());

        }

        parkCooperateService.createPark(parkCooperateDomain);

        parkCooperateDomain.setParkImg(img);
        parkCooperateDomain.setParkContent(content);
        EditPark(parkCooperateDomain, date1, date2);

        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    /**
     * 删除公园页面
     */
    @RequestMapping(value = R.DelPark, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.DelPark, viewName = DelPvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer delPark(@YomsArrayParam List<Integer> ids) throws YomsException {
        if (ids != null) {
            for (int id : ids) {
                parkCooperateService.deletePark(id);
                delParkImages(id, "content");
                delParkImages(id, "thumbnail");

            }
        }
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    /**
     * 删除公园相关图片
     *
     * @param
     */
    public void delParkImages(int id, String folder) {
        //获取图片路径
        File file = new File(YomsDefaultData.WebRoot, String.format("img/park/%s/%s", folder, id));
        if (file.exists()) {
            String imgPath = file.getAbsolutePath();
            FileHelper.delFolder(imgPath);
        }
    }


    /**
     * 重新编译
     *
     * @param parkCooperateDomain
     */
    private void recompileImg(ParkCooperateDomain parkCooperateDomain) {
        if (parkCooperateDomain.getParkContent() != null) {
            parkCooperateDomain.setParkContent(YomsBase64Image.recompileImgSrc(
                    parkCooperateDomain.getParkContent(),
                    parkCooperateDomain.getParkId(),
                    "park/content").getValue());
        } else {
            parkCooperateDomain.setParkContent("");
        }

        String img = YomsBase64Image.restoreBase64code(parkCooperateDomain.getParkImg());
        if (parkCooperateDomain.getParkImg() != null &&
                !parkCooperateDomain.getParkImg().contains("default/default-news.png")) {
            String value = YomsBase64Image.recompileImgSrc(img, parkCooperateDomain.getParkId(), "park/thumbnail").getValue();
            parkCooperateDomain.setParkImg(YomsBase64Image.getSrcValue(value));
        } else {
            parkCooperateDomain.setParkImg("");
        }
    }
}
